package com.appsindy.reserv.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.appsindy.common.action.AppsindyBaseAction;
import com.appsindy.common.action.IManageTab;
import com.appsindy.common.json.JSONResultCommon;
import com.appsindy.common.util.DateUtils;
import com.appsindy.reserv.bo.HtGlobalListValue;
import com.appsindy.reserv.bo.HtRoom;
import com.appsindy.reserv.dao.GlobalListValueDAO;
import com.appsindy.reserv.dao.RoomDAO;
import com.opensymphony.xwork2.Action;

public class MasterRoomAction extends AppsindyBaseAction implements IManageTab {
	
	private HtRoom htRoom;
	private List<HtRoom> roomList;
	private String sortBy;
	
	private RoomDAO roomDAO;
	private GlobalListValueDAO globalListValueDAO;

	public String execute() {
		log.info("Init Master Room ...");
		
		List<HtGlobalListValue> roomTypes = getGlobalListValueDAO().getRoomTypes();
		if(roomTypes == null){
			roomTypes = new ArrayList<HtGlobalListValue>();
		}
		getSession().put("S_ROOM_TYPES", roomTypes);
		log.info("Init S_ROOM_TYPES ... " + roomTypes.size() + " records.");
		
		if(htRoom == null){
			htRoom = new HtRoom();
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("roomCode", htRoom.getRoomCode());
		params.put("roomZone", htRoom.getRoomZone());
		params.put("roomFloor", htRoom.getRoomFloor());
		params.put("sortBy", getSortBy());
		List<HtRoom> roomList = getRoomDAO().searchRoom(params);
		setRoomList(roomList);
		
		//setMenuLinkList(getAuthorizationServices().getMenuLinkByLevel(1));
		getSession().put(S_TAB_CODE, MNG_ROOM);
		return SUCCESS;
	}
	
	public String getRoomDetail(){
		Map<String, Object> errorMap = new HashMap<String, Object>();
        errorMap.put("errorStr", "Cannot get room info");
		JSONResultCommon jsonResultCommon = new JSONResultCommon(Action.ERROR, errorMap);
		//String json = null;
		try{
        	HtRoom room = getRoomDAO().selectRoomById(htRoom.getRoomId());
        	/*json = "{'roomCode':'" + room.getRoomCode() + "'"
	             + ",'roomDesc':'" + room.getRoomDesc() + "'"
	             + ",'roomZone':'" + room.getRoomZone() + "'"
	             + ",'roomFloor':'" + room.getRoomFloor() + "'"
	             + "}";*/
        	List<HtRoom> list = new ArrayList<HtRoom>();
        	if(room != null){
        		room.setStartDateStr(DateUtils.getDateStr(room.getStartDate(), false));
        		room.setEndDateStr(DateUtils.getDateStr(room.getEndDate(), false));
        		list.add(room);
        	}
        	jsonResultCommon = new JSONResultCommon(Action.SUCCESS, list);
        } catch(Exception ex){
            ex.printStackTrace();
        }
        setJsonModel(jsonResultCommon);
    	//setJsonModel(json);
		return JSON_RESULT;
	}
	
	public String save() {
		log.info("Save HtRoom getRoomId ... " + htRoom.getRoomId());
		if(htRoom.getRoomId() < 1){
			try{
				getRoomDAO().insertRoom(htRoom);
				log.info("Insert Success");
			} catch(Exception e){
				e.printStackTrace();
			}
		} else{
			try{
				getRoomDAO().updateRoom(htRoom);
				log.info("Update Success");
			} catch(Exception e){
				e.printStackTrace();
			}
		}
		StringBuilder jsonResult = new StringBuilder("{");
		jsonResult.append("'status':1");
		jsonResult.append("}");
		setJsonModel(jsonResult.toString());
		return JSON_RESULT;
	}
	
	public String delete() {
		log.info("Delete HtRoom Id = " + htRoom.getRoomId());
		StringBuilder jsonResult = new StringBuilder("{");
		if(htRoom.getRoomId() > 0){
			getRoomDAO().deleteRoom(htRoom.getRoomId());
			log.info("Delete Success");
			jsonResult.append("'status':1");
		} else{
			log.info("Delete Failed");
			jsonResult.append("'status':0");
		}
		jsonResult.append("}");
		setJsonModel(jsonResult.toString());
		return JSON_RESULT;
	}
	
	public List<HtRoom> getTableData() {
		if(htRoom == null){
			htRoom = new HtRoom();
		}
		log.info("Init HtRoom TableData ... Code = " + htRoom.getRoomCode() + ", Zone = " + htRoom.getRoomZone() + ", Floor = " + htRoom.getRoomFloor());
		List<HtRoom> resultList = new ArrayList<HtRoom>();
		//if(StringUtils.isEmpty(getGroupCodeStr()) && StringUtils.isEmpty(getGroupNameStr())
		//		&& StringUtils.isEmpty(getCardNumStr())){
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("roomCode", htRoom.getRoomCode());
			params.put("roomZone", htRoom.getRoomZone());
			params.put("roomFloor", htRoom.getRoomFloor());
			List<HtRoom> roomList = getRoomDAO().searchRoom(params);
			if(roomList != null){
				/*java.util.Iterator<HtRoom> list = roomList.iterator();
				while(list.hasNext()){
					HtRoom ht = list.next();
					HtRoom room = new HtRoom();
					room.setRoomId(ht.getRoomId());
					room.setOrganizationId(ht.getOrganizationId());
					room.setRoomDesc(ht.getRoomDesc());
					room.setRoomCode(ht.getRoomCode());
					room.setRoomZone(ht.getRoomZone());
					room.setRoomFloor(ht.getRoomFloor());
					room.setTypeOfRoomId(ht.getTypeOfRoomId());
					room.setMaximumGuest(ht.getMaximumGuest());
					room.setCount4Available(ht.getCount4Available());
					room.setEnableFlag(ht.getEnableFlag());
					room.setStartDate(DateUtils.trimDate(ht.getStartDate()));
					room.setEndDate(DateUtils.trimDate(ht.getEndDate()));
					resultList.add(room);
				}*/
				resultList = roomList;
			}
		//}
		log.info("Init Finished ... " + resultList.size() + " rows");
        return resultList;
    }
	
	public List<HtRoom> getRoomList(){
        return roomList;
    }
    public void setRoomList(List<HtRoom> roomList){
        this.roomList = roomList;
    }
	
	public HtRoom getHtRoom() {
		return htRoom;
	}
	public void setHtRoom(HtRoom htRoom) {
		this.htRoom = htRoom;
	}

	public RoomDAO getRoomDAO() {
		return roomDAO;
	}
	public void setRoomDAO(RoomDAO roomDAO) {
		this.roomDAO = roomDAO;
	}
	
	public void setGlobalListValueDAO(GlobalListValueDAO globalListValueDAO) {
		this.globalListValueDAO = globalListValueDAO;
	}
	public GlobalListValueDAO getGlobalListValueDAO() {
		return globalListValueDAO;
	}

	public String getSortBy() {
		return sortBy;
	}

	public void setSortBy(String sortBy) {
		this.sortBy = sortBy;
	}
	
}
